package com.nl.countdownclock.client;
import com.google.gwt.canvas.dom.client.Context2d;
import com.nl.countdownclock.client.PhysicalEnsemble;

public class MatrixDigit {
	public static int HORZ = 4;
	public static int VERT = 7;
	
	public static int[][][] numbergrid = {
			
			{
		{1,1,1,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,1,1,1}
		},
		
		{
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1}
		},
		
		{
		{1,1,1,1},
		{0,0,0,1},
		{0,0,0,1},
		{1,1,1,1},
		{1,0,0,0},
		{1,0,0,0},
		{1,1,1,1}
		},
		
		{
		{1,1,1,1},
		{0,0,0,1},
		{0,0,0,1},
		{1,1,1,1},
		{0,0,0,1},
		{0,0,0,1},
		{1,1,1,1}
		},
		
		{
		{1,0,0,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,1,1,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1},
		},
		{
		{1,1,1,1},
		{1,0,0,0},
		{1,0,0,0},
		{1,1,1,1},
		{0,0,0,1},
		{0,0,0,1},
		{1,1,1,1}
		},
		{
		{1,1,1,1},
		{1,0,0,0},
		{1,0,0,0},
		{1,1,1,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,1,1,1}
		},
		{
		{1,1,1,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1},
		{0,0,0,1}
		},
		{
		{1,1,1,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,1,1,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,1,1,1}
		},
		{
		{1,1,1,1},
		{1,0,0,1},
		{1,0,0,1},
		{1,1,1,1},
		{0,0,0,1},
		{0,0,0,1},
		{1,1,1,1}
		},
		{
		{1,0,0,1},
		{0,1,1,0},
		{0,1,1,0},
		{0,1,1,0},
		{0,1,1,0},
		{0,1,1,0},
		{1,0,0,1}
		},	
		{
			{0,0,0,0},
			{0,0,0,0},
			{0,0,0,0},
			{0,1,1,0},
			{0,0,0,0},
			{0,0,0,0},
			{0,0,0,0}
			},		};
	Context2d ctx;
	double xul;
	double yul;
	double xspace;
	double yspace;
	double ballRadius;
	String fillStyle;
	String emptyFillStyle;
	int previousDigit;
	
	public MatrixDigit(Context2d context, double x, double y,  double xspacing, double yspacing, double ballradius, String ballcolor, String emptycolor){
		this.ctx = context;
		this.xul = x;
		this.yul = y;
		this.xspace = xspacing;
		this.yspace = yspacing;
		this.ballRadius = ballradius;
		this.fillStyle = ballcolor;
		this.emptyFillStyle = emptycolor;
		this.previousDigit = -1;
	}


	public void drawDigit(int n){
		int[][] grid = numbergrid[n];
		double x = xul;
		ctx.setFillStyle(emptyFillStyle);
		for( int i = 0; i < HORZ; i++, x+= xspace){
			double y = yul;
			for(int j = 0; j < VERT; j++, y+= yspace){
				if (previousDigit != -1 && previousDigit != n){
					//figure out which balls are not needed	
					if (numbergrid[previousDigit][j][i] > grid[j][i]){
						// ball at x, y, ballRadius, fillStyle should be put into PhysicalEnsemble
				    	Double m = 45.0;  // "blast" factor
				    	Double vx = m*(Math.random() - 0.5);
				    	Double vy = m*(Math.random() - 0.5);
						Ball b = new Ball( x,  y, vx, vy, ballRadius, fillStyle);
						PhysicalEnsemble.getPhysicalEnsemble().add(b);
						
					}
				}
				if(grid[j][i] != 1){
					ctx.beginPath();
					ctx.arc(x,y,ballRadius,0,Math.PI*2,true);
					ctx.closePath();
					ctx.fill();	
				}
			}
		}
		x = xul;
		ctx.setFillStyle(fillStyle);
		for( int i = 0; i < HORZ; i++, x+= xspace){
			double y = yul;
			for(int j = 0; j < VERT; j++, y+= yspace){
				if(grid[j][i] == 1){
					ctx.beginPath();
					ctx.arc(x,y,ballRadius,0,Math.PI*2,true);
					ctx.closePath();
					ctx.fill();	
				}
			}
		}
		previousDigit = n;
	}

	
}
